package com.example.a12;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a12.activity.LoginActivity;
import com.example.a12.activity.RegisterActivity;
import com.example.a12.activity.WeatherActivity;
import com.example.a12.adapter.ViewPagerAdapter;
import com.example.a12.fragment.MusicFragment;
import com.example.a12.fragment.NearbyFragment;
import com.example.a12.fragment.NewsFragment;
import com.example.a12.fragment.NovelFragment;
import com.example.a12.fragment.RecommendFragment;
import com.example.a12.fragment.VideosFragment;
import com.google.android.material.navigation.NavigationView;

import androidx.fragment.app.Fragment;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private ImageView menu;
    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    private ViewPagerAdapter viewPagerAdapter;
    private NavigationView navigationView;
    private TextView login, register;
    private ImageView weather;
    String[] titleArr = new String[]{"小说", "视频", "新闻", "音乐"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = findViewById(R.id.drawer_layout);
        menu = findViewById(R.id.menuDrawer);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager2 = findViewById(R.id.viewPager2);
        navigationView = findViewById(R.id.nav_view);
        View headerLayout = navigationView.getHeaderView(0);
        weather = headerLayout.findViewById(R.id.weather);
        login = headerLayout.findViewById(R.id.login);
        register = headerLayout.findViewById(R.id.register);

        menu.setOnClickListener(view -> drawerLayout.open());
        List<Fragment> fragmentList = new ArrayList<>();

        fragmentList.add(new NovelFragment());
        fragmentList.add(new VideosFragment());
        fragmentList.add(new NewsFragment());

        fragmentList.add(new MusicFragment());

        viewPagerAdapter = new ViewPagerAdapter(this, fragmentList, titleArr);
        viewPager2.setAdapter(viewPagerAdapter);

        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, true,
                ((tab, position) -> {
                    tab.setText(viewPagerAdapter.getTitle(position));
                }));
        tabLayoutMediator.attach();
        weather.setOnClickListener(view -> {
            Intent intentWeather = new Intent(this, WeatherActivity.class);
            startActivity(intentWeather);
        });

        login.setOnClickListener(view -> {
            Intent intentLogin = new Intent(this, LoginActivity.class);
            startActivity(intentLogin);
        });
        register.setOnClickListener(view -> {
            Intent intentRegister = new Intent(this, RegisterActivity.class);
            startActivity(intentRegister);
        });
    }
}